var urltest = sessionStorage.getItem("urltest");
var userId = sessionStorage.getItem("userId");
var fullname = sessionStorage.getItem("fullname");
//var organizeid = sessionStorage.getItem("organizeid");
$(function() {
	if(userId == "" || userId == undefined || userId == null) {
		top.location.href = "../login.html";
	} else {
		wuye_name()
		userId = userId;
		var pageNumber = "1";
		var pageSize = "13";
		var selectinfo = "";
		var reque = function() {
			return {
				merchantid: "",
				areaid: "",
				buildid: "",
				unitid: "",
				pageSize: pageSize,
				pageNumber: pageNumber,
				selectinfo: selectinfo
			}
		}
		rq1 = reque();
		var req = function() {
			return {
				requestString: "",
				userId: userId,
				fullname: fullname
			}
		}
		requestObj1 = req();
		requestObj1.requestString = JSON.stringify(rq1);
		var req3 = JSON.stringify(requestObj1);
		$.ajax({
			contentType: "application/json; charset=utf-8",
			type: "POST",
			url: urltest + '/verify/property/selectRommList',
			data: req3,
			async: false,
			dataType: "json",
			success: function(msg) {
				//				console.log(msg);
				if(msg.responseCode == 200) {
					$("#count").html(msg.count);
					var gezi = msg.count;
					if(gezi % 13 == 0) {
						var lenn = parseInt(gezi / 13);
						if(lenn == 0) {
							var lenn = 1;
						}
					} else {
						var lenn = parseInt(gezi / 13) + 1;
					}
					test("Test bootstrap v3 rendering", function() {
						var element = $('#bp-3-element-test');
						var options = {
							bootstrapMajorVersion: 3,
							currentPage: 1,
							numberOfPages: 5,
							totalPages: lenn
						}
						element.bootstrapPaginator(options);
						var element = $('#bp-3-element-test');
						ok(!element.hasClass('pagination-lg'), "Root element shouldn't have pagination-lg class");
						ok(!element.hasClass('pagination-sm'), "Root element shouldn't have pagination-sm class");
						var list = element.children();
						for(var i = 0; i < list.length; i++) {
							var item = $(list[i]);
							ok(item.is("li"), "Element " + i + " should be li");
						}
					});
					var table_msg = msg.returnString;
					createShowingTable(table_msg);
				} else {
					alert(msg.resoponseDisp)
				}
			},
			error: function() {
				alert("获取信息失败");
			}
		});
	}

});
//加载物业名称
function wuye_name(data) {
	var parentid = data;
	var reque = function() {
		return {
			parentid: parentid,
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/property/selectOrganizeNameList',
		data: req3,
		async: true,
		dataType: "json",
		success: function(msg) {
			//			console.log(msg);
			if(msg.responseCode == 200) {
				//				console.log(msg);
				//				console.log(msg.returnString);
				var bb = JSON.parse(msg.returnString);
				//				var str = "";
				var str = '<option value="">' + "请选择物业" + '</option>';
				for(var i = 0; i < bb.length; i++) {
					str += "<option value=" + bb[i].organizeid + ">" + bb[i].shortname + "</option>";
				}
				$("#select_wuye").html("");
				$("#select_wuye").append(str);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});
}
//物业改变  加载小区
$("#select_wuye").change(function() {
	var scdd_shxz = $(this).val();
	if(scdd_shxz == '') {
		var str = '<option value="">' + "请选择小区名称" + '</option>';
		$("#select_village").html("");
		$("#select_village").append(str);
		return
	}
	var data = JSON.stringify({
		requestString: JSON.stringify({
			merchantid: $(this).val()
		}),
		userId: userId,
		fullname: fullname
	})
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + '/verify/property/selectAreaName',
		data: data,
		async: true,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				//				console.log(msg);
				//				console.log(msg.returnString);
				var bb = JSON.parse(msg.returnString);
				//				var str = "";
				var str = '<option value="">' + "请选择小区" + '</option>';
				for(var i = 0; i < bb.length; i++) {
					str += "<option value=" + bb[i].areaid + ">" + bb[i].areaname + "</option>";
				}
				$("#select_village").html("");
				$("#select_village").append(str);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});
});
//小区改变出现楼宇
$("#select_village").change(function() {
	var data = JSON.stringify({
		requestString: JSON.stringify({
			areaid: $(this).val()
		}),
		userId: userId,
		fullname: fullname
	})
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + '/verify/property/selectBuildList',
		data: data,
		async: true,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				//				console.log(msg);
				//				console.log(msg.returnString);
				var bb = JSON.parse(msg.returnString);
				//				var str = "";
				var str = '<option value="">' + "请选择楼宇" + '</option>';
				for(var i = 0; i < bb.length; i++) {
					str += "<option value=" + bb[i].id + ">" + bb[i].buildname + "</option>";
				}
				$("#select_build").html("");
				$("#select_build").append(str);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});
});
//楼改变出现单元
$("#select_build").change(function() {
	var data = JSON.stringify({
		requestString: JSON.stringify({
			buildid: $(this).val()
		}),
		userId: userId,
		fullname: fullname
	})
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + '/verify/property/selectUnitsList',
		data: data,
		async: true,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				//				console.log(msg);
				//				console.log(msg.returnString);
				var bb = JSON.parse(msg.returnString);
				//				var str = "";
				var str = '<option value="">' + "请选择单元" + '</option>';
				for(var i = 0; i < bb.length; i++) {
					str += "<option value=" + bb[i].id + ">" + bb[i].unitname + "</option>";
				}
				$("#select_unit").html("");
				$("#select_unit").append(str);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});
});
$("#bp-3-element-test").on("click", function() {
	var aa = $(this).children(".active").children("a").html();
	var pageNumber = aa;
	var pageSize = "13";
	var search_main = $('#text_sousuo').val();
	var selectinfo = search_main;
	var search_main = $('#text_sousuo').val();
	var merchantid = $("#select_wuye").val();
	var areaid = $("#select_village").val();
	var buildid = $("#select_build").val();
	var unitid = $("#select_unit").val();
	var reque = function() {
		return {
			merchantid: merchantid,
			areaid: areaid,
			buildid: buildid,
			unitid: unitid,
			pageSize: pageSize,
			pageNumber: pageNumber,
			selectinfo: selectinfo
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + '/verify/property/selectRommList',
		data: req3,
		async: true,
		dataType: "json",
		success: function(msg) {
			//			console.log(msg);
			if(msg.responseCode == 200) {
				//				console.log(msg);
				$("#count").html(msg.count);
				var gezi = msg.count;
				if(gezi % 13 == 0) {
					var lenn = parseInt(gezi / 13);
					if(lenn == 0) {
						var lenn = 1;
					}
				} else {
					var lenn = parseInt(gezi / 13) + 1;
				}
				test("Test bootstrap v3 rendering", function() {
					var element = $('#bp-3-element-test');
					var options = {
						bootstrapMajorVersion: 3,
						currentPage: aa,
						numberOfPages: 5,
						totalPages: lenn
					}
					element.bootstrapPaginator(options);
					var element = $('#bp-3-element-test');
					ok(!element.hasClass('pagination-lg'), "Root element shouldn't have pagination-lg class");
					ok(!element.hasClass('pagination-sm'), "Root element shouldn't have pagination-sm class");
					var list = element.children();
					for(var i = 0; i < list.length; i++) {
						var item = $(list[i]);
						ok(item.is("li"), "Element " + i + " should be li");
					}
				});
				var table_msg = msg.returnString;
				createShowingTable(table_msg);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});

})

function createShowingTable(bb) {
	//获取后台传过来的jsonData,并进行解析  
	//此处需要让其动态的生成一个table并填充数据 
	if(bb.total == 0) {
		$("#tbbb").html("");
	} else {
		//		console.log(bb);
		var bb = JSON.parse(bb);
		var tableStr = "";
		var len = bb.length;
		if(len == 0) {
			var wuxiaoxi = '<p>' + "暂无内容" + '</p>'
			$("#tbbb").html(wuxiaoxi);
		} else {
			for(var i = 0; i < len; i++) {
				if(bb[i].createusername == undefined) {
					bb[i].createusername = "";
				}
				if(bb[i].createdate == undefined) {
					bb[i].createdate = "";
				}
				if(bb[i].floor == undefined) {
					bb[i].floor = "";
				}
				if(bb[i].custsex == 1) {
					bb[i].custsex = "男"
				} else if(bb[i].custsex == 0) {
					bb[i].custsex = "女"
				}
				var num = parseInt(i) + parseInt(1);
				tableStr = tableStr + '<tr>' +
					'<th>' + num + '</th>' +
					'<th>' + bb[i].merchantname + '</th>' +
					'<th>' + bb[i].areaname + '</th>' +
					'<th>' + bb[i].buildname + '</th>' +
					'<th>' + bb[i].units + '</th>' +
					'<th>' + bb[i].floor + '</th>' +
					'<th>' + bb[i].rommcode + '</th>' +
					'<th>' + bb[i].custname + '</th>' +
					'<th>' + bb[i].mobilephone + '</th>' +
					'<th data-toggle="modal" data-target="#myModal" style="color:blue;cursor: pointer" ;><span class="glyphicon glyphicon-edit"></span></th>' +
					'<th style="display:none">' + bb[i].custsex + '</th>' +
					'<th style="display:none">' + bb[i].innerarea + '</th>' +
					'<th style="display:none">' + bb[i].housorienid + '</th>' +
					'<th style="display:none">' + bb[i].poolarea + '</th>' + //公摊面积
					'<th style="display:none">' + bb[i].builtarea + '</th>' + //建筑面积
					'<th style="display:none;">' + bb[i].contactphone + '</th>' + //固定电话
					'<th style="display:none;">' + bb[i].createdate + '</th>' + //创建时间 
					'<th style="display:none;">' + bb[i].rommid + '</th>' + //房间id 
					'<th style="display:none;">' + bb[i].remaks + '</th>' + //备注 	
					'</tr>';
			}
			//将动态生成的table添加的事先隐藏的div中.  
			$("#tbbb").html(tableStr);
		}
		//点击详情
		$("#tbbb tr th:nth-child(10)").click(function() {
			$('#room_sex').html($(this).siblings().eq(9).html());
			$('#room_tn').html($(this).siblings().eq(10).html());
			$('#room_type').html($(this).siblings().eq(11).html());
			$('#room_gt').html($(this).siblings().eq(12).html());
			$('#room_jz').html($(this).siblings().eq(13).html());
			$('#room_tel').html($(this).siblings().eq(14).html());
			$('#room_date').html($(this).siblings().eq(15).html());
			$('#room_id').html($(this).siblings().eq(16).html());
			$('#room_remake').val($(this).siblings().eq(17).html());
		});
	}
}
//模糊查询
$('#search_chaxun').click(function() {
	var pageNumber = "1";
	var pageSize = "13";
	var search_main = $('#text_sousuo').val();
	var merchantid = $("#select_wuye").val();
	var areaid = $("#select_village").val();
	var buildid = $("#select_build").val();
	var unitid = $("#select_unit").val();
	var selectinfo = search_main;
	var reque = function() {
		return {
			merchantid: merchantid,
			areaid: areaid,
			buildid: buildid,
			unitid: unitid,
			pageSize: pageSize,
			pageNumber: pageNumber,
			selectinfo: selectinfo
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + '/verify/property/selectRommList',
		data: req3,
		async: true,
		dataType: "json",
		success: function(msg) {
			//			console.log(msg);
			if(msg.responseCode == 200) {
				$("#count").html(msg.count);
				var gezi = msg.count;
				if(gezi % 13 == 0) {
					var lenn = parseInt(gezi / 13);
					if(lenn == 0) {
						var lenn = 1;
					}
				} else {
					var lenn = parseInt(gezi / 13) + 1;
				}
				test("Test bootstrap v3 rendering", function() {
					var element = $('#bp-3-element-test');
					var options = {
						bootstrapMajorVersion: 3,
						currentPage: 1,
						numberOfPages: 5,
						totalPages: lenn
					}
					element.bootstrapPaginator(options);
					var element = $('#bp-3-element-test');
					ok(!element.hasClass('pagination-lg'), "Root element shouldn't have pagination-lg class");
					ok(!element.hasClass('pagination-sm'), "Root element shouldn't have pagination-sm class");
					var list = element.children();
					for(var i = 0; i < list.length; i++) {
						var item = $(list[i]);
						ok(item.is("li"), "Element " + i + " should be li");
					}
				});
				var table_msg = msg.returnString;
				createShowingTable(table_msg);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});

})
//查看房屋下属人员信息 打开新页面 传参
$(".room_user").click(function() {
	var rommid = $("#room_id").html();
	sessionStorage.setItem("rommid", rommid);	
	window.location.href ='room_people_information.html';
})